package com.cosina.game.crystallight.ui.mapselect;

import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.cosina.game.crystallight.CrystalLightActivity;

public class MapView extends ScrollView {
	public MapView(CrystalLightActivity activity,
			UpAndDownLayout upAndDownLayout) {
		super(activity);
		this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				UpAndDownLayout.UP_HEIGHT));

		LinearLayout layout = new LinearLayout(activity);
		layout.setLayoutParams(new LayoutParams(320, 480 * 4));
		layout.setOrientation(LinearLayout.VERTICAL);
		mapPartView[0] = new MapPartView(activity, upAndDownLayout, 0);
		mapPartView[1] = new MapPartView(activity, upAndDownLayout, 1);
		mapPartView[2] = new MapPartView(activity, upAndDownLayout, 2);
		mapPartView[3] = new MapPartView(activity, upAndDownLayout, 3);
		layout.addView(mapPartView[0]);
		layout.addView(mapPartView[1]);
		layout.addView(mapPartView[2]);
		layout.addView(mapPartView[3]);
		this.addView(layout);
	}
	
	MapPartView[] mapPartView = new MapPartView[4];

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_UP) {
			
			float totalY = ev.getY() + this.getScrollY();
			
			MotionEvent newMotionEvent = MotionEvent.obtain(ev);
			
			newMotionEvent.setLocation(ev.getX(), totalY%480);
			if(mapPartView[(int)(totalY/480)].dispatchTouchEvent(newMotionEvent)){
				return true;
			}
		}

		return super.onTouchEvent(ev);
	}

}
